// pages/components/gkCanvas/gkCanvas.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        canvasNode:undefined,
        canvasId:undefined,
    },

    /**
     * 组件的方法列表
     */
    methods: {
        setSize(width,height){
            this.setData({
                'canvasNode.canvas.width':width,
                'canvasNode.canvas.height':height,
                'canvasNode.canvas._width':width,
                'canvasNode.canvas._height':height,
            })
        },
        draw(callback){  
            callback(this.data.canvasNode.canvas,this.data.canvasNode.ctx,this.data.canvasNode.dpr)
        },
        getCanvasNode(){
            // console.log("canvas comp:",this.data.canvasNode)
            return {
                canvas:this.data.canvasNode.canvas,
                ctx:this.data.canvasNode.ctx,
                dpr:this.data.canvasNode.dpr
            }
        },
        getImageUrl(type="jpg",quaility=1,x=0,y=0,width=undefined,height=undefined){
            return new Promise((resolve,reject)=>{
                wx.canvasToTempFilePath({
                    x: x,
                    y: y,
                    width: width,
                    height: height,
                    destWidth: width,
                    destHeight: height,
                    canvas: this.data.canvasNode.canvas,
                    fileType: type,
                    quality:quaility,
                    success(res) {
                        resolve(res.tempFilePath);
                    },
                    fail(err){
                        reject(err);
                    }
                })
                
            })
            
        },
        toBase64(type="image/jpeg"){
            return this.data.canvasNode.canvas.toDataURL(type,1);
        },
        getNode(nodeId, fields) {
            return new Promise((resolve, reject) => {
                
                this.createSelectorQuery()
                    .select(nodeId)
                    .fields(fields)
                    .exec(res => {
                        if (res[0]) {
                            resolve(res[0]);
                        } else {
                            reject("可能节点ID不存在")
                        }
    
                    })
            })
        },
        /**获取canvas对象 */
        getCanvas(canvasId) {
            return new Promise((resolve, reject) => {
                this.getNode(canvasId, {
                    node: true
                }).then(res => {
                    let canvas = res.node;
                    let ctx = canvas.getContext("2d");
                    const dpr = wx.getSystemInfoSync().pixelRatio
                    resolve({
                        canvas,
                        ctx,
                        dpr
                    });
                });
            });
    
        },
    },
    lifetimes: {

        /**初始化动作 */
        attached() {
            // 生成canvas id
            this.setData({
                canvasId: Math.random().toString(36).substring(2)
            })
  
        },
        async ready(){
            let canvasNode = await this.getCanvas(".mycanvas");
            this.setData({
                canvasNode:canvasNode
            })
            // this.getCanvas(".mycanvas").then(res=>{
            //     console.log("ready:",res)
            //     this.setData({
            //         canvasNode:res
            //     })
            // })
        }
    }
})
